package com.yanqu.road.server.cmd.activity.huashigang;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.huashigang.HuaShiGangProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.huashigang.HuaShiGangModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.logic.battle.BattleManager;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.huashigang.HuaShiGangMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = GameProtocol.S_HUA_SHI_GANG_AUTO_HANDLE_NEXT_POINT, desc = "自动处理下个点")
public class AutoHandleNextPointCmd extends AbstractCommand {

    private int x;
    private int y;

    public void init(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        HuaShiGangProto.AutoHandleNextPointReqMsg reqMsg = HuaShiGangProto.AutoHandleNextPointReqMsg.parseFrom(packet.getBytes());
        x = reqMsg.getX();
        y = reqMsg.getY();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ret = HuaShiGangMgr.getActivityInfoRet(player);
        if (ret != 0) {
            return;
        }

        // 活动期间内才能操作
        ActivityInfo activityInfo = HuaShiGangMgr.getActivityInfo();
        if (!ActivityMgr.activityInTime(activityInfo)) {
            ret = GameErrorCode.E_HUA_SHI_GAG_NOT_IN_FIGHT_TIME;
            return;
        }

        if (!HuaShiGangMgr.inOperateTime(activityInfo)) {
            ret = GameErrorCode.E_HUASHIGANG_NOT_OPERATE_TIME;
            return;
        }

        long goldenKeyNum = player.getModule(BagModule.class).getUserBagItemCount(HuaShiGangMgr.getHuaShiGangGoldKeyId());


        HuaShiGangProto.CrossAutoHandleNextPointReqMsg.Builder builder = HuaShiGangProto.CrossAutoHandleNextPointReqMsg.newBuilder();
        builder.setX(x);
        builder.setY(y);
        builder.setActivityId(activityInfo.getActivityId());
        builder.setGoldenKeyNum((int) goldenKeyNum);
        builder.setPlayerBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        // 门客ID、赚钱、职业加成
        List<UserPatrons> list = new ArrayList<>();
        for (UserPatrons pat : player.getModule(PatronsModule.class).getUserPatronsList()) {
            list.add(pat.cloneUserPatrons());
        }
        BattleManager.skillEffect(list);
        for (UserPatrons userPatrons : list) {
            builder.addPatronsId(userPatrons.getPatronsId());
            builder.addPatronsAbility(userPatrons.getAbility().longValue());
            builder.addPatronsAddition(userPatrons.getBattleAddition());
        }
        builder.setTotalAbility(player.getModule(HuaShiGangModule.class).getPatronsAbility());
        player.sendPacket(CrossProtocol.C2_HUA_SHI_GANG_AUTO_HANDLE_NEXT_POINT, builder);

    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            HuaShiGangProto.AutoHandleNextPointRespMsg.Builder builder = HuaShiGangProto.AutoHandleNextPointRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_HUA_SHI_GANG_AUTO_HANDLE_NEXT_POINT, builder);
        }
    }
}
